 /*
 * @description: 店铺数据/商品改价记录
 * @Author: along
 * @Date: 2021-05-24
 * @Last Modified by: along
 * @Last Modified time: 2021-05-24
 */
<template>
    <el-dialog
        title="商品改价记录"
        :visible.sync="dialogVisible"
        width="776px"
        :close-on-click-modal="false"
        class="__model"
    >
        <!-- 筛选-->
        <div class="filter-view">
            <div class="header-new-box">
                <p class="header-new-box-title">商品信息：</p>
                <el-input
                    v-model="keyword"
                    placeholder="请输入商品标题或编号"
                    size="small"
                    clearable
                    suffix-icon="el-icon-search"
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                />
            </div>
            <div class="header-new-box">
                <p class="header-new-box-title">商品状态:</p>
                <el-select
                    v-model="status"
                    clearable
                    filterable
                    placeholder="请选择"
                    size="small"
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in statustList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
        </div>
        <div class="__table">
            <el-table
                :data="tableData"
                height="100%"
                style="width: 100%"
                class="data-table"
                @sort-change="handleSortChanged"
            >
                <el-table-column
                    type="index"
                    label="序号"
                    width="80"
                    align="center"
                />
                <el-table-column
                    prop="goods_title"
                    label="商品名称"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="left"
                />
                <el-table-column
                    prop="status"
                    label="商品状态"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="left"
                >
                    <template slot-scope="scope">
                        <p :style="{color: 'rgba(51, 51, 51, 1)'}">
                            {{ scope.row.status | filterStatus }}
                        </p>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="change_count"
                    label="改价次数"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="left"
                    sortable="custom"
                >
                    <template slot-scope="scope">
                        <el-popover
                            placement="bottom"
                            width="600"
                            trigger="hover"
                        >
                            <template>
                                <div class="__provide">
                                    <p class="__provide_title">
                                        改价记录
                                    </p>
                                    <div class="__provide_items">
                                        <div
                                            v-for="(row,index) in dataList"
                                            :key="index"
                                            class="__items"
                                        >
                                            <p class="__time">
                                                {{ row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                                            </p>
                                            <template v-if="row.list.length">
                                                <div class="__option">
                                                    <div
                                                        v-for="(el,i) in row.list"
                                                        :key="i"
                                                        class="__option_items"
                                                    >
                                                        规格({{ el.goods_specs | filterSpec }}),学生价由<span class="__number">¥{{ el.goods_price }}</span>改为<span class="__number">¥{{ el.goods_new_price }}</span>
                                                    </div>
                                                </div>
                                            </template>
                                        </div>
                                    </div>
                                </div>
                            </template>
                            <el-button
                                slot="reference"
                                type="text"
                                :style="{
                                    color: 'rgba(32, 160, 255, 1)',
                                    cursor: 'pointer',
                                    border: 'none'
                                }"
                            >
                                <p @mouseenter="handlegetpricelist(scope.row)">{{ scope.row.change_count }}</p>
                            </el-button>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="last_update_time"
                    label="最近一次改价时间"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="left"
                    sortable="custom"
                >
                    <template slot-scope="scope">
                        {{ scope.row.last_update_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- /分页 -->
        <div class="pagination-wrapper clearfixNew">
            <el-pagination
                layout="total, prev, pager, next, jumper"
                background
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
    </el-dialog>
</template>

<script>
export default {
    name: 'CheckShopSale',
    data () {
        return {
            shop_no: '',
            keyword: '',
            status: 99,
            dialogVisible: false,
            tableData: [],
            statustList: [{value: 99, label: '全部'},{value: -1, label: '已删除'},{value: 0, label: '已下架'},{value: 1, label: '已上架'}],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            filterData: {
                sort_field: 0,
                sort_type: 1
            },
            h_page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            dataList: []
        };
    },
    filters: {
        filterStatus (val) {
            if(val === 0) return '已下架';
            if(val === -1) return '已删除';
            if(val === 1) return '已上架';
        },
        filterSpec (val) {
            if(!val.length) return '--';
            let str = '';

            val.map((el, index) => {
                str += el.spec_name + ':' + el.spec_value + ';';
            });

            return str;
        }
    },
    methods: {
        /**
         * @description 显示弹窗
         */
        show (row) {
            this.shop_no = row.shop_no;
            this.getTableData(1);
            this.dialogVisible = true;
        },

        /**
         * @description 关闭弹窗
         */
        close () {
            this.dialogVisible = false;
        },

        /**
         * @description 获取表格数据
         */
        getTableData(page) {
            this.$post('/student/statistic_shop%5Cget_shop_goods_change_price_list', {
                page_id: page || this.page.now_page,
                shop_no: this.shop_no,
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type,
                keyword: this.keyword,
                status: this.status
            }, resp => {
                if (resp.code === 1) {
                    this.tableData = resp.data.list;
                    this.page = {
                        ...resp.data.page
                    };
                } else {
                    this.$notify({
                        title: '失败',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * @description 获取改价记录
         */
        handlegetpricelist (row) {
            this.$post('/student/statistic_shop%5Cget_shop_goods_product_change_price_list', {
                page_id: this.h_page.now_page,
                goods_no: row.goods_no
            }, resp => {
                if (resp.code === 1) {
                    let list = resp.data.list;
                    let newList = [];

                    if(!list.length) return false;
                    for(let i=0; i<list.length; i++) {
                        newList.push({
                            create_time: list[i]['change_time'],
                            list: []
                        });

                        const change_list = list[i]['change_list'];

                        if(change_list && !!change_list.length) {
                            for(let j=0; j<change_list.length; j++) {
                                newList[i]['list'].push({
                                    goods_specs: change_list[j]['goods_specs'],
                                    // spec_value: change_list[j]['spec_value'],
                                    // spec_name: change_list[j]['spec_name'],
                                    goods_price: change_list[j]['old_product_price'],
                                    goods_new_price: change_list[j]['now_product_price']
                                });
                            }
                        }
                    }

                    this.dataList = newList;

                    this.h_page = {
                        ...resp.data.page
                    };
                } else {
                    this.$notify({
                        title: '失败',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * @description 数据排序
         */
        handleSortChanged(data) {
            let sortTypeMap = {
                ascending: 0,
                descending: 1
            };

            let sortFieldMap = {
                change_count: 1,
                last_update_time: 2
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = '';
                this.filterData.sort_type = '';
            }

            this.getTableData();
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
.__table {
    width: 100%;
    max-height: 424px;
    overflow: hidden;
    overflow-y: auto;
    box-sizing: border-box;
    border-left: 1px solid #EBEEF5;
    border-right: 1px solid #EBEEF5;
}
.pagination-wrapper {
    padding: 0px 24px;
    border: 1px solid #EBEEF5;
    display: flex;
    align-items: center;
    height: 56px;
    border-top: none;
    justify-content: flex-end;
    .el-pagination {
        float: right;
        padding: 0;
        background-color: transparent;
    }
}

.filter-view {
    margin-bottom: 24px;
    display: flex;
    align-items: center;

    .header-new-box {
        display: flex;
        align-items: center;
        color: #606266;
        white-space: nowrap;
        margin-right: 32px;

        &-title {
            margin-right: 2px;
            white-space: nowrap;
        }
    }
}
.__provide {
    display: flex;
    flex-direction: column;
    align-items: center;
    .__provide_title {
        color: rgba(51, 51, 51, 1);
        font-size: 16px;
        font-weight: 500;
    }
    .__provide_items {
        width: 100%;
        border: 1px rgba(238, 238, 238, 1) solid;
        border-radius: 4px;
        box-sizing: border-box;
        padding: 32px 32px 12px 32px;
        margin-top: 16px;
        background: rgba(255, 255, 255, 1);
        max-height: 400px;
        overflow: hidden;
        overflow-y: auto;
        &::-webkit-scrollbar {
            width: 2px;
        }
        .__items {
            display: flex;
            align-items: flex-start;
            margin-bottom: 32px;
            .__time {
                width: 153px;
                text-align: left;
            }
            .__option {
                flex: 1;
                display: flex;
                flex-direction: column;
                .__option_items {
                    display: flex;
                    align-items: center;
                    color: rgba(51, 51, 51, 1);
                    font-size: 14px;
                    margin-bottom: 16px;
                    span {
                        color: rgba(247, 144, 56, 1);
                        font-size: 14px;
                    }
                }
                .__option_items:last-child {
                    margin-bottom: 0px;
                }
            }
        }
        .__items:last-child {
            // margin-bottom: 0px;
        }
    }
}
</style>